package com.axend.aerosense.room.viewmodel;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.utils.SystemUtil;
import com.axend.aerosense.room.entity.RoomNameBean;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.network.subsciber.IProgressDialog;
import com.axend.aerosense.room.R;

import io.reactivex.disposables.Disposable;

public class RoomSetNameViewModel extends BaseViewModel {
    public MutableLiveData<String> roomName = new MutableLiveData<>();
    private Disposable disposable;

    @Override
    public void onLoad() {

    }

    public void setRoomName(Activity activity, RoomNameBean roomNameBean, IOperationDetailResult<String> operationResultListence){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if(TextUtils.isEmpty(token) || null == roomNameBean){
            return;
        }
        disposable = EasyHttp.post(UrlApi.URL.SetRoomName.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(roomNameBean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        ToastUtils.showShort(e.getMessage());
                        if(operationResultListence != null){
                            operationResultListence.onFail(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_success);
                        if(operationResultListence != null){
                            operationResultListence.onSuccess("");
                        }
                    }
                });
    }

    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
        }
        super.onCleared();
    }
}
